package cn.com.thingo.hyintelligent.presenter;


import javax.inject.Inject;

import cn.com.thingo.hyintelligent.base.RxPresenter;
import cn.com.thingo.hyintelligent.base.contract.DoorAuthContract;
import cn.com.thingo.hyintelligent.model.DataManager;
import cn.com.thingo.hyintelligent.model.bean.CommonBean;
import cn.com.thingo.hyintelligent.model.bean.DoorLockAuthBean;
import cn.com.thingo.hyintelligent.util.LogUtil;
import cn.com.thingo.hyintelligent.util.RxUtil;
import cn.com.thingo.hyintelligent.util.ToastUtil;
import io.reactivex.functions.Consumer;

public class DoorAuthPresenter extends RxPresenter<DoorAuthContract.View> implements DoorAuthContract.Presenter {

    private DataManager dataManager;

    @Inject
    public DoorAuthPresenter(DataManager dataManager) {
        this.dataManager = dataManager;
    }


    @Override
    public void addDoorAuth(String token, String gatewayId, final String telephone) {
        addSubscribe(dataManager.addDoorLockAuth(token, gatewayId, telephone)
                .compose(RxUtil.<CommonBean>rxSchedulerHelper())
                .subscribe(new Consumer<CommonBean>() {
                    @Override
                    public void accept(CommonBean commonBean) throws Exception {
                        if (commonBean.getStatus() == 1) {
                            mView.viewAddDoorAuth(commonBean);
                        } else {
                            ToastUtil.shortShow(commonBean.getMsg());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        LogUtil.e(throwable.getMessage());
                    }
                }));

    }

    @Override
    public void delDoorAuth(String token, String gatewayId, String telephone) {
        addSubscribe(dataManager.delDoorLockAuth(token, gatewayId, telephone)
                .compose(RxUtil.<CommonBean>rxSchedulerHelper())
                .subscribe(new Consumer<CommonBean>() {
                    @Override
                    public void accept(CommonBean commonBean) throws Exception {
                        if (commonBean.getStatus() == 1) {
                            mView.viewAddDoorAuth(commonBean);
                        } else {
                            ToastUtil.shortShow(commonBean.getMsg());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        LogUtil.e(throwable.getMessage());
                    }
                }));
    }

    @Override
    public void doorLockAuthList(String token, String gatewayId) {
        addSubscribe(dataManager.doorLockAuthList(token, gatewayId)
                .compose(RxUtil.<DoorLockAuthBean>rxSchedulerHelper())
                .subscribe(new Consumer<DoorLockAuthBean>() {
                    @Override
                    public void accept(DoorLockAuthBean doorLockAuthBean) throws Exception {
                        if (doorLockAuthBean.getStatus() == 1) {
                            mView.viewDoorLockAuthList(doorLockAuthBean);
                        } else {
                            ToastUtil.shortShow(doorLockAuthBean.getMsg());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        LogUtil.e(throwable.getMessage());
                    }
                }));

    }
}
